package migrations

import (
	"go-caipu/pkg/services/sqlstore/migrations/admin"
	"go-caipu/pkg/services/sqlstore/migrations/common"
	"go-caipu/pkg/services/sqlstore/migrations/plugins"
	"go-caipu/pkg/services/sqlstore/migrations/storage"
	"go-caipu/pkg/services/sqlstore/migrations/tools"
	. "go-caipu/pkg/services/sqlstore/migrator"
)

type OSSMigrations struct {
}

func ProvideOSSMigrations() *OSSMigrations {
	return &OSSMigrations{}
}

// AddMigration create table
func (*OSSMigrations) AddMigration(mg *Migrator) {
	addMigrationLogMigrations(mg)
	admin.AddApiMigrations(mg)
	admin.AddUserMigrations(mg)
	admin.AddRoleMigrations(mg)
	admin.AddPostMigrations(mg)
	admin.AddOperaLogMigrations(mg)
	admin.AddMenuMigrations(mg)
	admin.AddLoginLogMigrations(mg)
	admin.AddDictTypeMigrations(mg)
	admin.AddDictDataMigrations(mg)
	admin.AddDeptMigrations(mg)
	admin.AddConfigMigrations(mg)
	admin.AddNoticeMigrations(mg)
	//admin.AddCasbinRuleMigrations(mg)
	common.AddLoginAttemptMigrations(mg)
	common.AddOnlineUserMigrations(mg)

	//tools
	//if mg.Dialect.DriverName() == "mysql" {
	tools.AddSysToolsTableMigrations(mg)
	tools.AddSysToolsColumnMigrations(mg)
	//}
	//plugins data source
	plugins.AddPluginDataSourceMigrations(mg)
	
	//file
	storage.AddFileStorageMigrations(mg)
	storage.AddFileMetadataMigrations(mg)
	storage.AddStorageConfigMigrations(mg)

}

// addMigrationLogMigrations 执行生成表结构记录表
func addMigrationLogMigrations(mg *Migrator) {
	migrationLogV1 := Table{
		Name: "sys_migration_log",
		Columns: []*Column{
			{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
			{Name: "migration_id", Type: DB_NVarchar, Length: 255},
			{Name: "sql", Type: DB_Text},
			{Name: "success", Type: DB_Bool},
			{Name: "error", Type: DB_Text},
			{Name: "timestamp", Type: DB_DateTime},
		},
	}
	mg.AddMigration("create table sys_migration_log", NewAddTableMigration(migrationLogV1))
}
